<?php

require_once(WSB_XTENSION_PATH . 'flex/models/Table.php');

class Flex_IndexController extends WSB_Xtension_Extension
{

	public function indexAction()
	{
	    $flexValues = $this->_getFlexValues();
	    $this->_view->assign($flexValues);

        $this->_view->output = $this->_getFlexDefinitionOutput();

		$this->renderView('index.phtml');
	}

	public function getTable()
	{
		return new Flex_Model_Table();
	}

	public function editorUrl($idContainer, array $globals)
	{
	    return Cms_Url::getUrl('index', 'flex', array('id' => $idContainer), 'xtflex', $globals);
	}

	private function _getFlexValues()
	{
        $tmpValues = unserialize($this->_container->flex);
        $values = array();
        if(is_array($tmpValues)) {
            foreach($tmpValues AS $key=>$value) {
                if(substr($key, 0, 1) != '_') {
                    $values[$key] = trim($value);
                }
            }
        }
        return $values;
	}

	private function _getFlexDefinitionOutput()
	{
		$table = new Flex_Model_Table();
        $rowset = $table->find($this->_container->item_id);
        if($rowset->count() >= 1) {
            $row = $rowset->current();
            if(!empty($row)) {
                return $row->output;
            }
        }
        return array();
	}

}
